package com.jilian.mydemo.modul.shop.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jilian.mydemo.R;
import com.jilian.mydemo.common.dto.ProductDetailDto;
import com.jilian.mydemo.common.dto.ShopDto;
import com.jilian.mydemo.common.dto.ShopListDto;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.ScreenUtils;

import java.util.List;


public class ShopCardAdapter extends RecyclerView.Adapter<ShopCardAdapter.ViewHolder> {
    private Activity mContext;
    private List<ProductDetailDto> datas;
    private CustomItemClickListener listener;
    private CheckListener checkListener;
    public ShopCardAdapter(Activity context, List<ProductDetailDto> datas, CustomItemClickListener listener,CheckListener checkListener) {
        mContext = context;
        this.datas = datas;
        this.listener = listener;
        this.checkListener = checkListener;
    }
    public interface  CheckListener{
        void check(int position);
        void add(int position);
        void del(int position);
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_shoppingcart, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, listener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (EmptyUtils.isNotEmpty(datas.get(position).getProductImageList())) {
            Glide.with(mContext).load(datas.get(position).getProductImageList().get(0)).into(holder.ivHead);
        }
        if (datas.get(position).isCheck()) {
            holder.ivCheck.setImageResource(R.drawable.image_cart_icon_choosed);
        } else {
            holder.ivCheck.setImageResource(R.drawable.image_cart_icon_unchoosed);
        }
        holder.ivCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkListener.check(position);
            }
        });
        holder.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkListener.add(position);
            }
        });
        holder.ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkListener.del(position);
            }
        });

        holder.tvName.setText(datas.get(position).getProductName());
        holder.tvType.setText(datas.get(position).getProductType());
        holder.tvPrice.setText(datas.get(position).getPrice());
        holder.tvActualprice.setText(datas.get(position).getActualPrice());
        holder.tvCount.setText(datas.get(position).getQuantity());


    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivCheck;
        private ImageView ivHead;
        private TextView tvName;
        private TextView tvType;
        private TextView tvPrice;
        private TextView tvActualprice;
        private ImageView ivDel;
        private TextView tvCount;
        private ImageView ivAdd;


        public ViewHolder(final View itemView, final CustomItemClickListener listener) {
            super(itemView);
            ivCheck = (ImageView) itemView.findViewById(R.id.iv_check);
            ivHead = (ImageView) itemView.findViewById(R.id.iv_head);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvType = (TextView) itemView.findViewById(R.id.tv_type);
            tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
            tvActualprice = (TextView) itemView.findViewById(R.id.tv_actualprice);
            ivDel = (ImageView) itemView.findViewById(R.id.iv_del);
            tvCount = (TextView) itemView.findViewById(R.id.tv_count);
            ivAdd = (ImageView) itemView.findViewById(R.id.iv_add);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onItemClick(itemView, getAdapterPosition());
                }
            });
        }

    }
}
